import plotly.plotly as py
import plotly.graph_objs as go
from scipy.io import loadmat
import numpy as np
from matplotlib import cm
import matplotlib
from scikits import audiolab

py.sign_in('wuxiaominupc', '6jv5jrgvxs')


def matplotlib_to_plotly(cmap, pl_entries):
    h = 1.0 / (pl_entries - 1)
    pl_colorscale = []

    for k in range(pl_entries):
        C = map(np.uint8, np.array(cmap(k * h)[:3]) * 255)
        pl_colorscale.append([k * h, 'rgb' + str((C[0], C[1], C[2]))])
    return pl_colorscale


def show_line():
    mat_0 = '/home/xiaomin/wxm/Data/KaggleSeizure2016/mats/resample600w400_NoCorr/train_35.mat'
    mat_1 = '/home/xiaomin/wxm/Data/KaggleSeizure2016/mats/resample600w400_NoCorr/train_36.mat'

    mat_0_x = loadmat(mat_0)['x']
    mat_1_x = loadmat(mat_1)['x']

    print mat_0_x.shape
    print mat_1_x.shape

    trace = go.Scatter(
        x=mat_0_x[0],
        y=np.asarray([i for i in range(600)])
    )
    data = [trace]
    py.plot(data, filename='KaggleSeizure2016Mat')


def show_heatmap():
    mat_0 = '/home/xiaomin/wxm/Data/KaggleSeizure2016/mats/resample600w400_NoCorr/train_4217.mat'
    mat_1 = '/home/xiaomin/wxm/Data/KaggleSeizure2016/mats/resample600w400_NoCorr/train_4211.mat'

    mat_0_x = loadmat(mat_0)['x']
    mat_1_x = loadmat(mat_1)['x']

    print mat_0_x.shape
    print mat_1_x.shape

    data = [go.Heatmap(z=mat_1_x)]
    py.plot(data, filename='heatmap_train_4211_1')


def show_heatmap_matplotlib():
    magma_cmap = matplotlib.cm.get_cmap('magma')
    magma = matplotlib_to_plotly(magma_cmap, 255)

    mat_0 = '/home/xiaomin/wxm/Data/KaggleSeizure2016/mats/resample600w400_NoCorr/train_4217.mat'
    mat_1 = '/home/xiaomin/wxm/Data/KaggleSeizure2016/mats/resample600w400_NoCorr/train_4211.mat'

    mat_0_x = loadmat(mat_0)['x']
    mat_1_x = loadmat(mat_1)['x']

    print mat_0_x.shape
    print mat_1_x.shape

    data = [go.Heatmap(z=mat_1_x, colorscale=magma, showscale=False)]
    py.plot(data, filename='heatmapPlt_train_4211_1')


def show_wav_waves():
    wav_path = '/home/xiaomin/wxm/Data/KaggleSeizure2016/wavs/SubjPerChannel/train_1/1_1_0_e0.wav'
    wav = audiolab.wavread(wav_path)
    w = wav[0][0:1000]
    trace = go.Scatter(
        y=w,
        x=np.asarray([i for i in range(len(w))])
    )
    data = [trace]
    py.plot(data, filename='KaggleSeizure2016Wav')


if __name__ == '__main__':
    show_wav_waves()
